package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.Font;

import javax.swing.SwingConstants;

import java.awt.Color;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Input extends JInternalFrame {
	Connection conn;
	
	
	private JTextField inputcode;
	private JLabel label;
	private JLabel label_1;
	private JTextField inputea;
	private JButton btnNewButton;
	private JLabel inputstatus;


	public Input(Connection conn) {
		super("데이타 추가", true, true, true, true);
		this.conn = conn;
		getContentPane().setBackground(new Color(255, 102, 0));
		setTitle("데이타 입력");
		setBounds(0, 0, 270, 121);
		getContentPane().setLayout(null);
		getContentPane().add(getInputcode());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getInputea());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getInputstatus());

	}
	private JTextField getInputcode() {
		if (inputcode == null) {
			inputcode = new JTextField();
			inputcode.setBounds(59, 10, 108, 21);
			inputcode.setColumns(10);
		}
		return inputcode;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드 :");
			label.setFont(new Font("굴림", Font.BOLD, 15));
			label.setForeground(new Color(51, 255, 0));
			label.setBounds(10, 10, 50, 20);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("수량 :");
			label_1.setForeground(new Color(51, 255, 0));
			label_1.setFont(new Font("굴림", Font.BOLD, 15));
			label_1.setBounds(10, 37, 50, 20);
		}
		return label_1;
	}
	private JTextField getInputea() {
		if (inputea == null) {
			inputea = new JTextField();
			inputea.setColumns(10);
			inputea.setBounds(59, 37, 108, 21);
		}
		return inputea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("등록");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						if(inputcode.getText().equals("") || inputea.getText().equals("")){
							inputstatus.setText("공백없이 입력하세요.");
						}
						else{
							String co = inputcode.getText();
							int ee = Integer.parseInt(inputea.getText());
							String sql = "insert into pro(serial, code, ea) values(pro_serial.nextval,?,?)";
							PreparedStatement pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, co);
							pstmt.setInt(2, ee);
							
							pstmt.executeUpdate(); // 입력 수정 삭제 할때 전부
							System.out.println("껒");
							inputstatus.setText("데이터가 등록되었습니다.");
							inputcode.setText("");
							inputea.setText("");
							inputcode.requestFocus();
							
							pstmt.close();
						}
						
					}catch(Exception ex){
						ex.printStackTrace();
						inputstatus.setText("제대로 입력해라");
					}
				}
			});
			btnNewButton.setForeground(new Color(255, 0, 0));
			btnNewButton.setFont(new Font("굴림", Font.BOLD, 15));
			btnNewButton.setBackground(new Color(255, 255, 0));
			btnNewButton.setBounds(179, 9, 65, 50);
		}
		return btnNewButton;
	}
	private JLabel getInputstatus() {
		if (inputstatus == null) {
			inputstatus = new JLabel("");
			inputstatus.setHorizontalAlignment(SwingConstants.CENTER);
			inputstatus.setFont(new Font("굴림", Font.BOLD, 15));
			inputstatus.setBounds(10, 63, 229, 21);
		}
		return inputstatus;
	}
}
